
import { getRandomDefaultUser, uniConfig } from "../config/UniConfig";
import { randomGenUserId } from "../core/CommUtil";
import { Databus } from "../core/Databus";
import { ResourceManager } from "../framework/ResourceManager";
import { querySettingList } from "./Setting";
import { updateUserHeader } from "./UserInfo";

const databus = Databus.Instance();
function updateUserAttribute(body: any) {
  databus.selfInfo.setUserInfo(body['user']);
  if (databus.selfInfo.nickName === '' || databus.selfInfo.avatarUrl === '') {
    const ua = getRandomDefaultUser(databus.selfInfo.userId);
    databus.selfInfo.nickName = ua.nickName;
    databus.selfInfo.avatarUrl = ua.avatarUrl;
    updateUserHeader(databus.selfInfo.userId, databus.selfInfo.nickName, databus.selfInfo.avatarUrl);
  }
}
// 微信平台用户登录请求
export function wxUserLogin(code: string) {
  const url = `${uniConfig.apiUrl}/unigame/wx/billiard/login`;
  const xhr = new XMLHttpRequest();
  console.log('request url ', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
      updateUserAttribute(response);
      databus.updateUserAttribute();
      ResourceManager.Instance().loadImageSpriteFrame(
        ResourceManager.SelfUserHeaderImage,
        databus.selfInfo.avatarUrl,
        ResourceManager.Instance().getImageExt(databus.selfInfo.avatarUrl),
      );

      querySettingList(databus.selfInfo.userId, databus.selfInfo.userId, 0);
    }
  };
  xhr.setRequestHeader("Content-Type", "application/json");
  // xhr.open("GET", url, true);
  xhr.open("POST", url, true);
  const data = {
    code: code,
  };
  xhr.send(JSON.stringify(data));
}
// 微信平台用户登录请求
export function ByteDanceUserLogin(code: string) {
  const url = `${uniConfig.apiUrl}/unigame/bytedance/billiard/login`;
  const xhr = new XMLHttpRequest();
  console.log('bytedance request url ', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
      updateUserAttribute(response);
      databus.updateUserAttribute();
      ResourceManager.Instance().loadImageSpriteFrame(
        ResourceManager.SelfUserHeaderImage,
        databus.selfInfo.avatarUrl,
        ResourceManager.Instance().getImageExt(databus.selfInfo.avatarUrl),
      );
      console.log('bytedance login ', response);
      querySettingList(databus.selfInfo.userId, databus.selfInfo.userId, 0);
    }
    console.log('bytedance login text', xhr.responseText);
  };
  xhr.setRequestHeader("Content-Type", "application/json");
  // xhr.open("GET", url, true);
  xhr.open("POST", url, true);
  const data = {
    code: code,
  };
  xhr.send(JSON.stringify(data));
}

export function htmlUserLogin() {
  const response = {
    user: {
      userId: randomGenUserId(),
      nickName: 'Web用户',
      avatarUrl: uniConfig.defaultXAvatarUrl,
      noviceTraining: 1,
      playLevel: 10,
      goldCoin: 10000,
      winNum: 20,
      loseNum: 10,
      tieNum: 0,
      modifyTime: new Date().toDateString(),
      createTime: new Date().toDateString(),
    }
  };
  ResourceManager.Instance().loadImageSpriteFrame(
    ResourceManager.SelfUserHeaderImage,
    databus.selfInfo.avatarUrl,
    ResourceManager.Instance().getImageExt(databus.selfInfo.avatarUrl),
  );
}

export function byteDanceUserLogin() {
  const userInfo = getRandomDefaultUser('123');
  databus.selfInfo.nickName = userInfo.nickName;
  databus.selfInfo.avatarUrl = userInfo.avatarUrl;
  ResourceManager.Instance().loadImageSpriteFrame(
    ResourceManager.SelfUserHeaderImage,
    databus.selfInfo.avatarUrl,
    ResourceManager.Instance().getImageExt(databus.selfInfo.avatarUrl),
  );
}


    